#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.base.rabbit;

import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RabbitMsgProducerBase<T> {
    @Resource(name = "rabbitTemplate")
    @Lazy
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String exchange, String routingKey, T msg) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }

    public void sendMsg(String exchange, String routingKey, T msg, Long timestamp) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setHeader("_HQ_TIMESTAMP", timestamp);
            return message;
        });
    }

    public void sendMsg(String exchange, String routingKey, T msg, Long timestamp, Integer ttl) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setHeader("_HQ_TIMESTAMP", timestamp);
            if (ttl != null) {
                message.getMessageProperties().setExpiration(String.valueOf(ttl));
            }
            return message;
        });
    }

    /**
     * 发送持久化消息
     */
    public void sendMsg(String exchange, String routingKey, T msg, Integer ttl, Boolean isPersistent) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            if (ttl != null) {
                message.getMessageProperties().setExpiration(String.valueOf(ttl));
            }
            message.getMessageProperties().setDeliveryMode(isPersistent ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT);
            return message;
        });
    }

    public void sendMsg(String exchange, String routingKey, T msg, Boolean isPersistent) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setDeliveryMode(isPersistent ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT);
            return message;
        });
    }
    /**
     * 发送带唯一标识的消息
     */
    public void sendMsg(String exchange, String routingKey, T msg, String messageId, Boolean isPersistent) {
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, message -> {
            message.getMessageProperties().setMessageId(messageId);
            message.getMessageProperties().setDeliveryMode(isPersistent ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT);
            return message;
        });
    }

    /**
     * 发送广播消息
     */
    public void sendMsg(String exchange, T msg, Boolean isPersistent) {
        rabbitTemplate.convertAndSend(exchange, "", msg, message -> {
            message.getMessageProperties().setDeliveryMode(isPersistent ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT);
            return message;
        });
    }
}
